package com.hzkq.widgets.chart.simple.pseudo3d;

import android.graphics.Paint.Align;
import android.os.Bundle;
import android.widget.Button;

import com.hzkq.widgets.R;
import com.keqiang.chart.impl.axis.Label;
import com.keqiang.chart.impl.axis.XAxis3D;
import com.keqiang.chart.impl.axis.YAxis;
import com.keqiang.chart.impl.axis.impl.bar.BarData;
import com.keqiang.chart.impl.axis.impl.bar.BarEntity;
import com.keqiang.chart.impl.axis.impl.bar.BarGroup;
import com.keqiang.chart.impl.axis.impl.pseudo3d.bar.BarChart3D;
import com.keqiang.chart.impl.axis.impl.pseudo3d.bar.BarChart3DHighLightRender;
import com.keqiang.chart.impl.axis.impl.pseudo3d.bar.BarChart3DView;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;
import me.zhouzhuo810.magpiex.ui.widget.TitleBar;
import me.zhouzhuo810.magpiex.utils.SimpleUtil;

/**
 * @author Created by wanggaowan on 2022/7/26 15:00
 */
public class BarChartActivity extends BaseActivity {
    
    private TitleBar mTitleBar;
    private BarChart3D mBarChart;
    private Button mBtRefresh;
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_bar_chart_2_5d;
    }
    
    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }
    
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.title_bar);
        BarChart3DView view = findViewById(R.id.bar_chart);
        mBarChart = view.getChart();
        mBtRefresh = findViewById(R.id.bt_refresh);
        
        initBarChart();
    }
    
    @Override
    public void initData() {
        fillBarChartData();
    }
    
    @Override
    public void initEvent() {
        mTitleBar.getLlLeft().setOnClickListener(v -> closeAct());
        
        mBtRefresh.setOnClickListener(v -> fillBarChartData());
    }
    
    private void initBarChart() {
        XAxis3D xAxis = mBarChart.getXAxis();
        xAxis.setDrawGridLine(true);
        xAxis.setGridUseEndPadding(true);
        xAxis.setLabelAlign(Align.CENTER);
        xAxis.setLabelOverAlign(Align.LEFT);
        xAxis.setLabelCount(6);
        xAxis.setLabelMaxLines(2);
        xAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        xAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        
        YAxis yAxis = mBarChart.getYAxis();
        yAxis.setDrawGridLine(true);
        yAxis.setGridUseEndPadding(true);
        yAxis.setLabelAlign(Align.RIGHT);
        yAxis.setLabelMaxLines(2);
        yAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        yAxis.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxis.setLabelCount(6);
        yAxis.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        yAxis.setMin(0);
        yAxis.setMax(100);
        
        xAxis.setAxisValueFormat(integer -> Label.obtain((integer + 1) + "月"));
        
        mBarChart.setHighLightRender(new BarChart3DHighLightRender(mBarChart));
    }
    
    /**
     * 设置图表数据
     */
    private void fillBarChartData() {
        List<BarGroup> data = new ArrayList<>();
        int count = mBarChart.getXAxis().getLabelCount();
        YAxis yAxis = mBarChart.getYAxis();
        for (int i = 0; i < count; i++) {
            List<BarEntity> entityList = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                BarEntity entity = new BarEntity((float) (yAxis.getMin() + Math.random() * (yAxis.getMax() - yAxis.getMin())));
                entity.setDrawLabel(true);
                entity.setLabel(((int) entity.getValue()) + "");
                if (j == 0) {
                    entity.setWidthPercent(0.2f);
                    entity.setColor(0xff61F2AD);
                } else if (j == 1) {
                    entity.setWidthPercent(0.2f);
                    entity.setColor(0xffA2E4FC);
                } else {
                    entity.setWidthPercent(0.2f);
                    entity.setColor(0xffFF6600);
                }
                entityList.add(entity);
            }
            BarGroup barData = new BarGroup(entityList);
            barData.setBarOffset(SimpleUtil.getScaledValue(5));
            data.add(barData);
        }
        
        BarData barData = new BarData();
        barData.setGroups(data);
        mBarChart.setData(barData);
        mBarChart.animDraw(2000);
    }
}
